package com.example.ssss;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.ssss.data.IotdbOperation;
import com.example.ssss.data.MyMqttService;
import com.example.ssss.data.MysqlOperation;
import com.example.ssss.data.entity.MyMqttMessage;
import com.example.ssss.data.entity.ShareData;
import com.example.ssss.data.entity.TimeStamp;
import com.example.ssss.databinding.ActivityMainBinding;
import com.example.ssss.state.HomeStateViewModel;

import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class MainActivity extends AppCompatActivity {
    private Intent mIntent;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();
        String userId = intent.getStringExtra("userId");
        ShareData shareData = (ShareData)getApplication();
        if(shareData.userId == null) shareData.userId = userId;
//        SharedPreferences mySharedPreferences = getSharedPreferences("user",Activity.MODE_PRIVATE);
//        SharedPreferences.Editor editor = mySharedPreferences.edit();
//        if(shareData.userId == null) editor.putString("userId",userId);
//        editor.commit();
        HomeStateViewModel homeStateViewModel = new ViewModelProvider(this).get(HomeStateViewModel.class);
        homeStateViewModel.setUserId(userId);
        Toast.makeText(this,"下午好，黄先生",Toast.LENGTH_SHORT).show();


//        TimeStamp timeStamp = new TimeStamp();
//        timeStamp.setWearBeginTimeStamp(0);
//        timeStamp.setWearEndTimeStamp(120001);
//        timeStamp.setEnvBeginTimeStamp(0);
//        timeStamp.setEnvEndTimeStamp(120001);
//        timeStamp.setBehaveBeginTimeStamp(0);
//        timeStamp.setBehaveEndTimeStamp(120001);
//        timeStamp.setUserId(userId);
//        IotdbOperation iotdbOperation = new IotdbOperation();
//        try {
//            iotdbOperation.SessionStart(timeStamp);
//        } catch (IoTDBConnectionException e) {
//            e.printStackTrace();
//        } catch (StatementExecutionException e) {
//            e.printStackTrace();
//        }

        super.onCreate(savedInstanceState);

        ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.container);

        //隐藏顶部状态栏
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) { actionBar.hide(); }

        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_start, R.id.navigation_my)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(binding.navView, navController);
        mIntent = new Intent(MainActivity.this, MyMqttService.class);
        //开启服务
        startService(mIntent);

    }

}